namespace ScrumSprintMonitor.ConfigEditor.Converters
{
	#region #using Directives

	using System;
	using System.Globalization;
	using System.Windows.Data;

	#endregion

	public class PercentageValueConverter : IValueConverter
	{
		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value != null && value is double)
			{
				double val = (double) value;

				return string.Format("{0:0%}", val);
			}

			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value != null && value is string)
			{
				string text = (string) value;

				text = text.Trim();
				text = text.TrimEnd(new[] {'%'});
				if (!string.IsNullOrEmpty(text))
				{
					return double.Parse(text, NumberStyles.Float) / 100;
				}
			}

			return null;
		}

		#endregion
	}
}